package cn.huiyunche.admin.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.huiyunche.base.service.interfaces.SOrderService;
import cn.huiyunche.base.service.utils.ExcelExport;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;

/**
 * 客户订单Controller
 * @author Administrator
 *
 */
@RequestMapping("/order")
@Controller
public class OrderController {

	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
	
	@Autowired
	private SOrderService sOrderService = null;
	
	/**
	 * 60已发车(司机在承运端点击按钮)状态订单显示, (修改为70运输中)
	 * @param page
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/alreadystartlist", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> alreadystartList(PageVo page) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getSOrderService().alreadystartList(page));
		return result;
	}
	
	/**
	 * 80已交车状态订单显示，(修改为90已完成)
	 * @param page
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/completedlist", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> completedList(PageVo page) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getSOrderService().completedList(page));
		return result;
	}
	
	/**
	 * 修改订单运单状态－已发车
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/changestatus", method = RequestMethod.POST)
	@ResponseBody
	public Result<Object> changeStatus(Long id) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "修改订单状态成功");
		result.setData(this.getSOrderService().changeStatus(id));
		return result;
	}
	
	/**
	 * 修改订单运单状态－已收车－暂时后台修改不改变状态
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/changestatusdone", method = RequestMethod.POST)
	@ResponseBody
	public Result<Object> changeStatusOrderDone(Long id) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "修改订单状态成功");
		result.setData(this.getSOrderService().changeStatusOrderDone(id));
		return result;
	}
	
	/**
	 * 显示提车照片
	 * @param orderId
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/driverstart", method = RequestMethod.POST)
	@ResponseBody
	public Result<Object> selectTichePic(Long orderId) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getSOrderService().selectTichePic(orderId, false));
		return result;
	}
	
	/**
	 * 显示交车照片
	 * @param orderId
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/donepic", method = RequestMethod.POST)
	@ResponseBody
	public Result<Object> orderDonePic(Long orderId) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getSOrderService().selectTichePic(orderId, true));
		return result;
	}
	
	/**
	 * 确认收车 上传照片存储照片信息
	 * @param key
	 * @param type
	 * @param orderId
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/uploadimg", method = RequestMethod.POST)
	@ResponseBody
	public Result<Object> uploadimg(String key, int type, Long orderId) throws BusinessException {
		Result<Object> result = new Result<Object>(true, "确认收车保存数据成功");
		if(StringUtils.isEmpty(key) || type == 0 || orderId == null){
			result.setSuccess(false);
			result.setMessage("必填参数为空");
			return result;
		} 
		return this.getSOrderService().saveOrderAttach(key, type, orderId);
	}
	
	/**
	 * 拆单集单页面 订单显示
	 * @param page
	 * @return
	 * @throws BusinessException
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> list(PageVo page, SOrderInfoVo sorderinfo) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getSOrderService().list(page, sorderinfo));
		return result;
	} */
	
	/**
	 * 拆单集单页面 订单明细显示
	 * @param page
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/orderinfo", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> orderDetail(Long orderid) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getSOrderService().orderDetail(orderid));
		return result;
	}
	
	/**
	 * 车保信息明细列表
	 * @param page
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/carinsurancedetails", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> carinsurancedetails(PageVo page) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getSOrderService().selectCarInsuranceDetails(page));
		return result;
	}
	
	/**
	 * 车保信息明细列表－导出
	 * @param request
	 * @param response
	 */
    @RequestMapping(value = "/exportinfos", method = RequestMethod.GET)
	public void exportExcel(HttpServletRequest request,HttpServletResponse response){
    	logger.info("export carinsurancedetails init.");
		String excelName = "车保信息";
		ExcelExport.exprotBefore(request,response,excelName);
		OutputStream fOut = null;
		try {
			String[] titile={"出发日期","起运地","目的地","台数","司机姓名","司机电话","运单号","车辆信息",};
			List<String[]> listData=this.getSOrderService().exportCarInsurances(titile.length);
			Boolean[] isdigit={null,null,null,null,null,null,null,null};
			int[] columnWidth={40,28,28,30,35,35,28,100 };
			HSSFWorkbook workbook =ExcelExport.export(excelName, titile,listData,columnWidth,isdigit, "");
			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
			logger.error("export carinsurancedetails failure reason 1 :"+e1.getMessage());
			e1.getMessage();
		} catch (Exception e) {
			logger.error("export carinsurancedetails failure reason 2 :"+e.getMessage());
			e.getMessage();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
				e.getMessage();
			}
		}
		logger.info("export carinsurancedetails success!");
	}
    
	@RequestMapping(value = "/changeorderstatus", method = RequestMethod.POST)
	@ResponseBody
	public Result<Object> changeOrderStatus(Long orderId, int status) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "状态更新成功");
		result.setData(this.getSOrderService().changeOrderStatus(orderId, status));
		return result;
	}
	
	private SOrderService getSOrderService() {
		return this.sOrderService;
	}
	
}
